package com.example.demo.base;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2019/12/11.
 */
public class BaseDao extends SqlSessionDaoSupport {
    private static final Logger logger = LogManager.getLogger(BaseDao.class);
    @Autowired
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }

    public <T> List<T> selectList(Class<T> T, String mappId, Object arg){
        return (List<T>)this.getSqlSessionTemplate().selectList(mappId,arg);
    }
    public <T> List<T> selectList(Class<T> T, String mappId){
        return (List<T>)this.getSqlSessionTemplate().selectList(mappId,null);
    }



    public <T> T selectOne(Class<T> T,String mappId,Object arg){
        return (T)this.getSqlSessionTemplate().selectOne(mappId,arg);
    }

    protected int insert(String mappId,Object arg){
        return this.getSqlSessionTemplate().insert(mappId,arg) ;
    }

    protected int update(String mappId,Object arg){
        return this.getSqlSessionTemplate().update(mappId,arg) ;
    }
    protected int delete(String mappId,Object arg){
        return this.getSqlSessionTemplate().delete(mappId,arg) ;
    }



    public int countByNamedSql(String nameSqlId,Object arg){
        Integer num = this.selectOne(Integer.class,nameSqlId,arg) ;
        if(num==null){
            return 0;
        }
        return num;
    }

    public int deleteBatch(String nameSqlId,List list){
        int retVal = 0;
        for(Object obj:list){
            retVal+=this.delete(nameSqlId,obj) ;
        }
        return retVal;
    }
}
